package service

import (
	"gitee.com/ysl729895/go-mall/internal/model"
	"gitee.com/ysl729895/go-mall/internal/repository"
)

// PermissionService 权限业务逻辑接口，定义权限相关的增删改查操作
// 便于 controller 层调用，实现解耦
// 包括：创建、更新、删除、查询单个/全部权限
type PermissionService interface {
	// CreatePermission 创建新权限
	CreatePermission(name, description string) error
	// UpdatePermission 更新权限信息
	UpdatePermission(id uint, name, description string) error
	// DeletePermission 删除权限
	DeletePermission(id uint) error
	// GetPermissionByID 根据ID查询权限
	GetPermissionByID(id uint) (*model.Permission, error)
	// GetAllPermissions 查询所有权限
	GetAllPermissions() ([]model.Permission, error)
}

// permissionService 权限业务逻辑实现结构体
// 依赖 PermissionRepository

type permissionService struct {
	repo repository.PermissionRepository
}

// NewPermissionService 构造函数，注入 PermissionRepository
func NewPermissionService(repo repository.PermissionRepository) PermissionService {
	return &permissionService{repo: repo}
}

// CreatePermission 创建新权限
func (s *permissionService) CreatePermission(name, description string) error {
	permission := &model.Permission{Name: name, Description: description}
	return s.repo.Create(permission)
}

// UpdatePermission 更新权限信息
func (s *permissionService) UpdatePermission(id uint, name, description string) error {
	permission, err := s.repo.GetByID(id)
	if err != nil {
		return err
	}
	permission.Name = name
	permission.Description = description
	return s.repo.Update(permission)
}

// DeletePermission 删除权限
func (s *permissionService) DeletePermission(id uint) error {
	return s.repo.Delete(id)
}

// GetPermissionByID 根据ID查询权限
func (s *permissionService) GetPermissionByID(id uint) (*model.Permission, error) {
	return s.repo.GetByID(id)
}

// GetAllPermissions 查询所有权限
func (s *permissionService) GetAllPermissions() ([]model.Permission, error) {
	return s.repo.GetAll()
}
